package com.wwtech.epp.ui.activity.work.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.ProjectEntity;
import com.wwtech.epp.ui.activity.work.ProjectListFragment;
import com.wwtech.epp.ui.activity.work.contract.ProjectListContract;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of ProjectListFragment
 * @date 2017/09/15 12:56:42
 */
public class ProjectListPresenter implements ProjectListContract.ProjectListContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final ProjectListContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private ProjectListFragment mFragment;

    @Inject
    public ProjectListPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull ProjectListContract.View view, ProjectListFragment fragment) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mFragment = fragment;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getProjectListFromServer(String projectStatus, int requestPage, int onePageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", onePageSize + "");
        map.put("pageNum", requestPage + "");
        switch (projectStatus) {
            case "我的项目":
                Disposable disposable = httpAPIWrapper.getProjectOwn(ContainValue.userLogin.getData().getUserId() + "", map)
                        .subscribe(new Consumer<ProjectEntity>() {
                            @Override
                            public void accept(ProjectEntity projectEntity) throws Exception {
                                //isSuccesse
                                KLog.i("onSuccesse");
                                mView.setListData(projectEntity);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //onError
                                KLog.i("onError");
                                mView.closeProgressDialog();
                                throwable.printStackTrace();
                            }
                        }, new Action() {
                            @Override
                            public void run() throws Exception {
                                //onComplete
                                KLog.i("onComplete");
                            }
                        });
                mCompositeDisposable.add(disposable);
                break;
            case "参与项目":
                Disposable disposable1 = httpAPIWrapper.getProjectParticipants(ContainValue.userLogin.getData().getUserId() + "", map)
                        .subscribe(new Consumer<ProjectEntity>() {
                            @Override
                            public void accept(ProjectEntity projectEntity) throws Exception {
                                //isSuccesse
                                KLog.i("onSuccesse");
                                mView.setListData(projectEntity);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //onError
                                KLog.i("onError");
                                mView.closeProgressDialog();
                                throwable.printStackTrace();
                            }
                        }, new Action() {
                            @Override
                            public void run() throws Exception {
                                //onComplete
                                KLog.i("onComplete");
                            }
                        });
                mCompositeDisposable.add(disposable1);
                break;
            case "星标项目":
                Disposable disposable2 = httpAPIWrapper.getProjectStar(ContainValue.userLogin.getData().getUserId() + "", map)
                        .subscribe(new Consumer<ProjectEntity>() {
                            @Override
                            public void accept(ProjectEntity projectEntity) throws Exception {
                                //isSuccesse
                                KLog.i("onSuccesse");
                                mView.setListData(projectEntity);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //onError
                                KLog.i("onError");
                                mView.closeProgressDialog();
                                throwable.printStackTrace();
                            }
                        }, new Action() {
                            @Override
                            public void run() throws Exception {
                                //onComplete
                                KLog.i("onComplete");
                            }
                        });
                mCompositeDisposable.add(disposable2);
                break;

        }
    }

}